﻿using System.ComponentModel.DataAnnotations;
using dotnet_ef_admin.Src.Models;

namespace dotnet_ef_admin.Src.Services;

/// <summary>
///     这个接口定义了UserService的契约。这个接口指定了UserService必须实现的方法。
///     UserService负责为User模型提供增删改查操作。
/// </summary>
public interface IUserService {
    // 异步从数据库中获取所有用户。
    /// <returns>表示异步操作的Task，Task结果包含一个IEnumerable的User对象。</returns>
    Task<IEnumerable<User>> GetAllUsersAsync();

    /// <summary>
    /// 异步查找表单
    /// </summary>
    /// <param name="id">表单的唯一标识符</param>
    /// <returns>返回一个任务，该任务结果包含查找表单的操作结果</returns>
    Task<FindFormResult> FindFormAsync(int id);

    // 异步在数据库中创建一个新用户。
    /// <param name="user">表示要创建的用户的User对象。</param>
    /// <returns>表示异步操作的Task，Task结果包含创建的User对象。</returns>
    Task<User> CreateUserAsync(CreateUserRequest user);

    // 异步在数据库中更新现有用户。
    /// <param name="user">表示要更新的用户的User对象。</param>
    /// <returns>表示异步操作的Task，Task结果包含更新后的User对象。</returns>
    Task UpdateUserAsync(int id, UpdateUserRequest user);

    /// <summary>
    ///     异步删除多个实体。
    /// </summary>
    /// <param name="ids">要删除的实体的ID数组。</param>
    /// <returns>返回删除的实体数量。</returns>
    Task<int> DeleteManyAsync(int[] ids);

    /// <summary>
    ///     异步更新指定实体的密码。
    /// </summary>
    /// <param name="id">需要更新密码的实体ID。</param>
    /// <param name="request">包含新密码信息的请求对象。</param>
    /// <returns>返回密码更新操作的任务。</returns>
    Task UpdatePasswordAsync(int id, UpdatePasswordRequest request);
}

/// <summary>
/// 创建用户请求模型
/// </summary>
public class CreateUserRequest {
    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = string.Empty; // 默认值为空字符串
    /// <summary>
    /// 昵称
    /// </summary>
    public string Nickname { get; set; } = string.Empty; // 默认值为空字符串
    /// <summary>
    /// 部门ID
    /// </summary>
    public int DeptId { get; set; } = 0; // 默认值为0
    /// <summary>
    /// 角色ID数组
    /// </summary>
    public int[] RoleIds { get; set; } = []; // 默认值为空数组
    /// <summary>
    /// 用户状态
    /// </summary>
    public int? Status { get; set; } = null; // 默认值为null
    /// <summary>
    /// 密码
    /// </summary>
    public string Password { get; set; } = string.Empty; // 默认值为空字符串
    /// <summary>
    /// 性别
    /// </summary>
    public int? Gender { get; set; } = null; // 默认值为null
    /// <summary>
    /// 手机号
    /// </summary>
    public string Mobile { get; set; } = string.Empty; // 默认值为空字符串
    /// <summary>
    /// 电子邮件
    /// </summary>
    public string Email { get; set; } = string.Empty; // 默认值为空字符串
}

/// <summary>
/// 更新用户请求模型
/// </summary>
public class UpdateUserRequest : CreateUserRequest {
    /// <summary>
    /// 用户ID
    /// </summary>
    public int Id { get; set; } = 0;
}

/// <summary>
/// 查询所有用户结果模型
/// </summary>
public class FindAllUserResult {
    /// <summary>
    /// 用户ID
    /// </summary>
    public int Id { get; set; } = 0;
    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = "";
    /// <summary>
    /// 昵称
    /// </summary>
    public string Nickname { get; set; } = "";
    /// <summary>
    /// 手机号
    /// </summary>
    public string Mobile { get; set; } = "";
    /// <summary>
    /// 性别标签
    /// </summary>
    public string GenderLabel { get; set; } = "";
    /// <summary>
    /// 头像URL
    /// </summary>
    public string Avatar { get; set; } = "";
    /// <summary>
    /// 电子邮件
    /// </summary>
    public string Email { get; set; } = "";
    /// <summary>
    /// 用户状态
    /// </summary>
    public int? Status { get; set; } = null;
    /// <summary>
    /// 部门名称
    /// </summary>
    public string DeptName { get; set; } = "";
    /// <summary>
    /// 角色名称
    /// </summary>
    public string RoleNames { get; set; } = "";
    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreateTime { get; set; } = DateTime.Now;
}

/// <summary>
/// 用户角色模型
/// </summary>
public class UserRole {
    /// <summary>
    /// 角色ID
    /// </summary>
    public int RoleId { get; set; }
}

/// <summary>
/// 更新密码请求模型
/// </summary>
public class UpdatePasswordRequest {
    /// <summary>
    /// 密码
    /// </summary>
    [Required]
    public string Password { get; set; } = "";
}

/// <summary>
/// 查询表单结果模型
/// </summary>
public class FindFormResult {
    /// <summary>
    /// 用户ID
    /// </summary>
    public int Id { get; set; } = 0;
    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = string.Empty;
    /// <summary>
    /// 昵称
    /// </summary>
    public string Nickname { get; set; } = string.Empty;
    /// <summary>
    /// 手机号
    /// </summary>
    public string Mobile { get; set; } = string.Empty;
    /// <summary>
    /// 性别
    /// </summary>
    public int Gender { get; set; } = 0;
    /// <summary>
    /// 头像URL
    /// </summary>
    public string Avatar { get; set; } = string.Empty;
    /// <summary>
    /// 电子邮件
    /// </summary>
    public string Email { get; set; } = string.Empty;
    /// <summary>
    /// 用户状态
    /// </summary>
    public int Status { get; set; } = 0;
    /// <summary>
    /// 部门ID
    /// </summary>
    public int DeptId { get; set; } = 0;
    /// <summary>
    /// 角色ID数组
    /// </summary>
    public int[] RoleIds { get; set; } = Array.Empty<int>();
}